//
//  HeightViewController.m
//  CoolMove
//
//  Created by CA on 15/4/25.
//  Copyright (c) 2015年 CA. All rights reserved.
//

#import "HeightViewController.h"


static const NSInteger kMinHeight = 50;     // 最矮高度
static const NSInteger kMaxHeight = 240;    // 最高高度
static NSString * const kDefaultHeight = @"170";

@interface HeightViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (nonatomic, strong) NSArray *heightArr;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIView *popView;
@property (weak, nonatomic) IBOutlet UIView *indicatorView;
@property (weak, nonatomic) IBOutlet UILabel *titleDescriptionLabel;

@end

@implementation HeightViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureLocalization];
    [self.picker selectRow:[self.heightArr indexOfObject:kDefaultHeight] inComponent:0 animated:NO];
    [self addGestureRecognizer];
    [self updateThemeUI];
    [self registerNotification];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [MobClick beginLogPageView:@"HeightPage"];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    [MobClick endLogPageView:@"HeightPage"];
    
}

#pragma mark - Notification

- (void)registerNotification
{
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateThemeUI) name:kThemeManagerChangeThemeNotification object:nil];
}

#pragma mark - Getter/Setter

- (NSArray *)heightArr
{
    if (_heightArr == nil) {
        NSMutableArray *heights = [[NSMutableArray alloc] initWithCapacity:150];
        for (NSInteger index = kMinHeight; index <= kMaxHeight; index++) {
            [heights addObject:[NSString stringWithFormat:@"%ld", (long)index]];
        }
        _heightArr = [heights copy];
    }
    
    return _heightArr;
}

#pragma mark - IBAction

- (IBAction)cancel:(id)sender
{
    [self close];
}

- (IBAction)confirm:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(heightViewController:didSelectHeight:)]) {
        NSInteger heightIndex = [self.picker selectedRowInComponent:0];
        NSString *heightStr = self.heightArr[heightIndex];
        [self.delegate heightViewController:self didSelectHeight:heightStr];
    }
    
    [self close];
}

#pragma mark - Private Method

- (void)configureLocalization{
    self.titleDescriptionLabel.text = NSLocalizedString(@"Please choose height", nil);
    [self.okButton setTitle:NSLocalizedString(@"Ok", nil) forState:UIControlStateNormal];
    [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
}

- (void)updateThemeUI
{
//    self.okButton.backgroundColor = [[ThemeManager sharedInstance] pickerOkButtonColor];
//    self.popView.backgroundColor = [[ThemeManager sharedInstance] pickerBackgroundColor];
//    self.indicatorView.backgroundColor = [[ThemeManager sharedInstance] pickerSelectionIndicatorColor];
}

- (void)close
{
    [self dismissFromParentViewControllerWithAnimationType:DetailViewControllerAnimationTypeSlide];
}

#pragma mark - UIPicker DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.heightArr count];
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSAttributedString *attribuedString = [[NSAttributedString alloc] initWithString:self.heightArr[row] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:25.0f], NSForegroundColorAttributeName: [UIColor colorWithHex:0x7c7c7e]}];
    return attribuedString;
}

#pragma mark - Private Method

- (void)addGestureRecognizer
{
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
    gestureRecognizer.cancelsTouchesInView = NO;
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];
}
#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.view == touch.view;
}


@end
